# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  https://gitee.com/autumner/pythoncookbook
@software: PyCharm 
@file: net_ssl_2_flask_client.py 
@time: 2019/7/17 上午10:43
"""
'''
双向认证客户端
'''

from urllib.request import Request, urlopen
import ssl


if __name__ == '__main__':
    CA_FILE = "server_cert.pem"
    KEY_FILE = "client_key.pem"
    CERT_FILE = "client_cert.pem"

    context = ssl.SSLContext(ssl.PROTOCOL_TLS)
    context.check_hostname = False
    context.load_cert_chain(certfile=CERT_FILE, keyfile=KEY_FILE)
    context.load_verify_locations(CA_FILE)
    context.verify_mode = ssl.CERT_REQUIRED
    try:
        # 通过reques()方法创建一个请求
        req = Request('https://127.0.0.1:5000/')
        res = urlopen(req, context=context)
        print(res.code)
        print(res.read().decode('utf-8'))
    except Exception as ex:
        print("Found Error in auth phase:%s" % str(ex))
